/*
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.ui;

import wtkx.Component;
import wtkx.Effect;
import wtkx.co.ArrayList;

import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;


/**
 * List of {@link wtkx.Effect} used by {@link wtkx.Component}.
 * @author jdp
 */
public final class Effects
    extends ArrayList<Effect>
{

    public Effects() {
        super();
    }



    public Effects clone(){
        Effects clone = (Effects)super.clone();
        return clone;
    }
    public void destroy(){
        for (Effect child : this){
            child.destroy();
        }
        this.clear();
    }
    public final Rectangle2D.Float getBounds2D(Component component, Rectangle2D.Float bounds){

        for (Effect effect : this){
            Rectangle2D.Float inner = effect.getBounds(component);
            if (null != inner)
                bounds.add(inner);
        }
        return bounds;
    }
    public void layout(Component component){

        for (Effect child : this){
            child.layout(component);
        }
    }
    public Graphics2D prepare(Component component, Graphics2D g){
        boolean cond;
        for (Effect effect : this){
            if (effect instanceof Effect.Runs)
                cond = (((Effect.Runs)effect).isRunning());
            else
                cond = true;

            if (cond){
                g = effect.prepare(component, g);
            }
        }
        return g;
    }
    public void update(Graphics2D g){
        boolean cond;
        for (Effect effect : this){
            if (effect instanceof Effect.Runs)
                cond = (((Effect.Runs)effect).isRunning());
            else
                cond = true;

            if (cond){
                effect.update(g);
            }
        }
    }

    public Rectangle2D.Float getTransformedBounds(Component component, float x, float y,
                                                  float width, float height)
    {
        int count = 0;
        Rectangle2D.Float re = new Rectangle2D.Float();
        boolean cond = false;
        for (Effect effect : this) {

            if (effect instanceof Effect.Runs)
                cond = (((Effect.Runs)effect).isRunning());
            else
                cond = true;

            if (cond){
                AffineTransform transform = effect.getTransform(component);
                if (null != transform && (!transform.isIdentity())){

                    Rectangle2D.Float area = new Rectangle2D.Float(x, y, width, height);
                    Shape transformedShape = transform.createTransformedShape(area);
                    Rectangle2D tranformedBoundsIn = (Rectangle2D)transformedShape.getBounds2D();
                    Rectangle2D.Float tranformedBoundsOut = new Rectangle2D.Float();
                    Rectangle2D.intersect(effect.getBounds(component), tranformedBoundsIn, tranformedBoundsOut);

                    re.add(tranformedBoundsOut);
                    count += 1;
                }
            }
        }
        if (0 != count)
            return re;
        else
            return null;
    }
}
